package com.ytjj.qmyx.admin.service.impl;

import com.alibaba.excel.util.CollectionUtils;
import com.gexin.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.constants.SysConfigConstants;
import com.ytjj.common.model.YxAlipayConfig;
import com.ytjj.common.model.YxDomain;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.common.utils.SysConfigUtil;
import com.ytjj.qmyx.admin.dao.SysConfigDao;
import com.ytjj.qmyx.admin.model.request.SysConfigRequest;
import com.ytjj.qmyx.admin.model.response.ShortChainConfigResponse;
import com.ytjj.qmyx.admin.model.response.SysConfigResponse;
import com.ytjj.qmyx.admin.service.SysconfigService;
import com.ytjj.qmyx.mall.client.MallClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class SysConfigServiceImpl implements SysconfigService {
    @Autowired
    private MallClient mallClient;

    @Resource
    private SysConfigDao sysConfigDao;

    @Value("${short.platfrom.url:}")
    private String shortUrl;

    @Override
    public CommonPage<SysConfigResponse> querySysConfigList(SysConfigRequest sysConfigRequest) {
        PageHelper.startPage(sysConfigRequest.getPageNum(),sysConfigRequest.getPageSize());
        List<SysConfigResponse> page = sysConfigDao.querySysConfigList(sysConfigRequest);
        return CommonPage.restPage(page);
    }

    @Override
    public int insertSysConfig(SysConfigRequest sysConfigRequest) {
        sysConfigRequest.setCreateTime(new Date());
        return sysConfigDao.insertSysConfig(sysConfigRequest);
    }

    @Override
    public int updateSysConfig(SysConfigRequest sysConfigRequest) {
        sysConfigRequest.setUpdateTime(new Date());
        return sysConfigDao.updateSysConfig(sysConfigRequest);
    }

    @Override
    public List<ShortChainConfigResponse> shortChainRealmList() {
        String sysConfig = sysConfigDao.getSysConfig(SysConfigConstants.SHORT_CHAIN_REALM);
        if (StringUtils.isNotBlank(sysConfig)) {
            List<ShortChainConfigResponse> shortChainConfigResponses = JSONObject.parseArray(sysConfig, ShortChainConfigResponse.class);
            return CollectionUtils.isEmpty(shortChainConfigResponses) ? new ArrayList<>() : shortChainConfigResponses;
        }
        return null;
    }

    @Override
    public String getSysConfig(String key) {
        return sysConfigDao.getSysConfig(key);
    }

    @Override
    public SysConfigResponse getSysConfigInfo(String key) {
        return sysConfigDao.getSysConfigInfo(key);
    }

    @Override
    public List<SysConfigResponse> getPaySysConfig() {
       return  sysConfigDao.getPaySysConfig(Lists.newArrayList("WX_PAY_FLAG","ALI_PAY_FLAG"));
    }

    @Override
    public List<YxAlipayConfig> getAlipayConfig() {
        return sysConfigDao.getAlipayConfig();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateAlipayConfig(Integer id) {
        sysConfigDao.updateAlipayConfig(id);
        sysConfigDao.updateAlipayConfig2(id);
    }

    @Override
    public List<YxDomain> getDomainUrl() {
        return sysConfigDao.getDomainUrl();
    }

    @Override
    public void updateDomainUrl(Integer id) {


        YxDomain yxDomain = sysConfigDao.getDomainUrlById(id);
        Map<String, String> param = new HashMap<>();
        param.put("urlOld",yxDomain.getDomainUrl());
        param.put("urlNew",yxDomain.getUrlNew());
        String result= HttpClientUtil.doGet(shortUrl+"/admin/updateLongUrl",param);
        log.info("updateDomainUrl,{}",result);
        if (result != null && result.equals("ok")){
            sysConfigDao.updateDomainUrlState(id);
        }
    }

    @Override
    public List<String> getAlipayAccount() {
        return sysConfigDao.getAlipayAccount();
    }

}
